<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 5/8 0008
 * Time: 16:30
 */

namespace app\api\model;


use think\Model;

class Torder extends Model
{
//    protected $_validate = array(
//        //金额为分
//        array('ord_fee', 'require', '金额不能为空'),
//        //array('shop_name', 'require', '店名不能为空' ),
//        //array('pmt_name', 'require', '支付类型不能为空'),
//    );

    public function addOrder($uid)
    {
        $input =file_get_contents('php://input');
        //将$input decode成数组
        parse_str($input,$map);
     //   $map = request()->post();
        $data['shop_no'] = $uid;
        $data['ord_no'] = $this->orderno();
        $data['ord_add_time'] = time();
        $data['tmptoken']=$map["token"];
        $data["ord_trade_amount"]=$map["ord_trade_amount"];
        if (!$this->create($data)) {
            return ['status' => 0, 'message' => $this->getError()];
        } else {
            $res = $this->save($data);
            if ($res) {
                return ['status' => 1, 'ord_no' =>  $data['ord_no']];
            } else {
                return ['status' => 0, 'message' => 'error'];
            }
        }
    }

    public function getRefund()
    {
        $res = $this->where('ord_no="'.input('ord_no').'" or ttg_no="'.input('ord_no').'"')->find();
        //$a =$this->getLastSql();
        //file_put_contents('pos1.txt',$a);
        if(!$res){
            return ['status' => 0,
                'message' => '订单号不存在'];
        }else if (($res['ord_type']==2)&&($res['ord_status']==1)) {
            return ['status' => 0,
                'message' => '该订单已经退款'];
        } else {
            $map['refund_no'] = 'R' . $this->orderno();
            $map['ord_type'] = '2';    //退款
            $map['ord_status'] = '2';  //退款状态中,接受回调后更改状态
            $no = $this->where(['id' => $res['id']])->save($map);
            //$a= $this->getLastSql();
            //file_put_contents('pos.txt',$a);
            if ($no) {
                return $map['refund_no'];
            } else {
                return ['status' => 0,
                    'message' => '本地error'];
            }
        }
    }

    public function orderno()
    {
        //$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        $orderSn = (intval(date('Y')) - 2017) . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }

    //pos机收银订单记录
    public function getRecord($uid){
        $map['shop_no'] = $uid;
        $where=1;
        if(input('lastid')){
            //$map['id'] = ['lt',input('lastid')];
            $lastid=input('lastid');
            $where.=" and id <" . $lastid;
        }
        if(input('starttime')){
            $starttime = strtotime(input('starttime'));
        }else{
            $starttime =0;
        }

        $endtime = time();
        if(input('endtime')){
            $endtime = strtotime(input('endtime'))+60*60*24;
        }

        //$map['ord_add_time'] = ['between',[$starttime,$endtime]];
        $where.=" and ord_add_time >= $starttime and ord_add_time < $endtime and ord_status=1";
        $res = $this->where($map)->where($where)->field('id,pmt_name as typename,ord_trade_amount as totalfee,ord_add_time as createtime ,ord_fee as servicefee,ord_type as typeid,ord_status as status')->order(' id desc')->limit(20)->select();
        //$res = $this->getLastSql();
        return $res;
    }

    //后台订单记录
    public function getOrder($rows){
        //  $map['shop_no'] = $uid; ?!


        if(input('starttime')){
            $starttime = strtotime(input('starttime'));
        }else{
            $starttime = 0;
        }

        $endtime = time();
        if(input('endtime')){
            $endtime = strtotime(input('endtime'))+60*60*24;
        }

        if(input('idno')){
            $no =input('idno');
            $map['_query'] = "ord_no=$no&ttg_no=$no&_logic=or";
        }

        $map['ttg_no'] = array('neq','0');
        $nowpage = input('page', 1);


        $map['ord_add_time'] = ['between',[$starttime,$endtime]];
        $totalpage = ceil(($this->where($map)->count()) / $rows);
        $list = $this->where($map)->limit(($nowpage - 1) * $rows, $rows)->select();
        $res  = [$totalpage,$list];
        // dump($res);
        //dump($this->getLastSql());
        return $res;
    }



    public function getInfo(){
        $map['id'] = input('id');
        $res =  $this->where($map)->field('id,ttg_no,pmt_name as typename,ord_type as typeid,ord_trade_amount as realfee,ord_add_time as createtime ,ord_fee as servicefee,ord_status as status,ord_no as orderno,shop_name as storename,ord_original_amount as orifee ,ord_discount_amount as freefee')->find();
        return $res;
    }

    public function getInfoNew(){
        $map['ord_no'] = input('out_no');
        $res =  $this->where($map)->field('id,ttg_no,pmt_name as typename,ord_type as typeid,ord_trade_amount as realfee,ord_add_time as createtime ,ord_fee as servicefee,ord_status as status,ord_no as orderno,shop_name as storename,ord_original_amount as orifee ,ord_discount_amount as freefee')->find();
        return $res;
    }

    public function getaData($uid){
        if(input('pickdate')){
            $pickdate = strtotime(input('pickdate'));
            //$map['ord_add_time'] = ['lt',$pickdate];
            $pickdate2=$pickdate+86399;
            $where="ord_add_time >= $pickdate and ord_add_time <= $pickdate2";
        }else{
            $pickdate = strtotime(date('Y-m-d'),time());
            $pickdate2=$pickdate+86399;
            //$map['ord_add_time'] = ['between',[strtotime(date('Y-m-d')),time()]];
            $where="ord_add_time >= $pickdate and ord_add_time <= $pickdate2";
        }
        $map['shop_no'] = $uid;
        //$temp['_string'] = "(ord_type = 1 and ord_status =1) or (ord_type = 2 and ord_status =2)";  //成功条件
        $temp['_string'] = "(ord_type = 1 and ord_status =1)";  //成功条件
        $temp1['_string'] = "(ord_type = 1 or ord_type =2)";   //所有的
        $temp2['_string'] = "(ord_type = 2 and ord_status =1)";  //失败条件
        //$temp2['_string'] = "(ord_status =4) or (ord_type = 2 and ord_status =1)";  //失败条件
        $data['totalassets'] = $this->where($map)->where($where . " and ord_type=1 and ord_status=1")->sum('ord_trade_amount');
        $res1 = $this->getLastSql();
        //$temp1['']
        $data['ordernum'] = $this->where($map)->where($where . " and ord_type=1 and ord_status=1")->count();  //总条数
        $data['detail']['successmoney'] = $this->where($map)->where($where . " and ord_type=1 and ord_status=1")->sum('ord_trade_amount');  //成功金额
        $data['detail']['successcount'] = $this->where($map)->where($where . " and ord_type=1 and ord_status=1")->count();  //成功总条数

        $data['detail']['falimoney'] = $this->where($map)->where($where . " and ord_type=2 and ord_status=1")->sum('ord_trade_amount');
        //$data['detail']['failcount'] = $data['ordernum']-$data['detail']['successcount'];
        $data['detail']['failcount'] = $this->where($map)->where($where . " and ord_type=2 and ord_status=1")->count();  //失败总条数
        return $data;

    }

}